package com.vnp.cooking.UIcontrols.tab;

import android.app.Dialog;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.TextView;

public class TabFragment extends Fragment {
	private String mPreviousScrTag;
	public static final String PRE_SCR_TAG = "PRE_SCR_TAG";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(getArguments() != null) {
			mPreviousScrTag = getArguments().getString(PRE_SCR_TAG);
		}
	}
	
	public void refresh() {
	}
	
	// dau's
	public boolean isBack() {
		if (getActivity() != null) return true;
		return false;
	}
	
	public void onBack() {
		if (getActivity() == null) return;
		if(mPreviousScrTag != null) {
			Fragment previousFragment = getActivity().getSupportFragmentManager().findFragmentByTag(mPreviousScrTag);
			if(previousFragment != null) {
				FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
				ft.remove(this);
				ft.attach(previousFragment);
				ft.commit();
			}
		} else {
			//if (getActivity() instanceof MainActivity) {
//				Typeface typefaceBold = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-BoldCondensed.ttf");
//				Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-Condensed.ttf");
//				final Dialog dialog = new Dialog(getActivity(), R.style.UpdateWindow);
//				dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//				dialog.setContentView(R.layout.close_app_layout);
//				// Set postion
//				WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
//				layoutParams.width = LayoutParams.WRAP_CONTENT; layoutParams.height = LayoutParams.WRAP_CONTENT;
//				layoutParams.gravity = Gravity.CENTER;
//				dialog.getWindow().setAttributes(layoutParams);
//				//dialog.getWindow().setBackgroundDrawableResource(R.drawable.update_background);
//				TextView title = (TextView) dialog.findViewById(R.id.title);
//				title.setTypeface(typefaceBold);
//				title.setText(getActivity().getResources().getString(R.string.app_name));
//				TextView content = (TextView) dialog.findViewById(R.id.content);
//				content.setTypeface(typeface);
//				content.setText("Bạn muốn đóng ứng dụng?");
//				Button btnOk = (Button) dialog.findViewById(R.id.btn_ok);
//				btnOk.setTypeface(typeface);
//				btnOk.setOnClickListener(new OnClickListener() {
//					
//					@Override
//					public void onClick(View v) {
//						onDestroy();
//						System.runFinalizersOnExit(true);
//						System.exit(0);
//					}
//				});
//				Button btnCancel = (Button) dialog.findViewById(R.id.btn_cancel);
//				btnCancel.setTypeface(typeface);
//				btnCancel.setOnClickListener(new OnClickListener() {
//					
//					@Override
//					public void onClick(View arg0) {
//						dialog.dismiss();
//					}
//				});
//				dialog.show();
			//} else {
				getActivity().finish();
			//}
		}
	}
	
	@Override
	public void onResume() {
		super.onResume();
		setCurTabFragment();
	}
	
	protected void setCurTabFragment() {
		((ITabHost) getActivity()).setCurTabFragment(this);
	}
}
